package datastructure.ch03_linked.ch11_single_circle;

import datastructure.ch03_linked.ch07_nodes.SingleNode;
import datastructure.ch03_linked.ch08_interface_abstract.AbstractLinkedList;

/**
 * 单向循环链表
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class SingleCircleLinkedList<E> extends AbstractLinkedList<E> {
    public static void main(String[] args) {
        SingleCircleLinkedList<Integer> data = new SingleCircleLinkedList<>();
        data.add(0, 1);
        data.add(1, 2);
        data.add(2, 3);
        data.add(3, 4);
        data.add(4, 5);
        data.add(5, 6);
        System.out.println(data.remove(0));
        System.out.println(data);
    }

    SingleNode<E> first;

    @Override
    public int indexOf(E element) {
        SingleNode<E> node;
        if (element == null) {
            node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            node = first;
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) return i;
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == 0) {
            // 思考：first = new SingleNode<>(element, first);
            SingleNode<E> newFirst = new SingleNode<>(element, first);
            SingleNode<E> last = (size == 0) ? newFirst : getNode(size - 1); // add opt
            last.next = newFirst; // add opt
            first = newFirst; // add opt
        } else {
            SingleNode<E> prev = getNode(index - 1);
            prev.next = new SingleNode<>(element, prev.next);
        }
        size++;
    }

    @Override
    public E remove(int index) {
        SingleNode<E> oldElement = first;
        if (index == 0) {
            if (size == 1) {
                first = null;
            } else {
                SingleNode<E> last = getNode(size - 1);
                first = first.next;
                last.next = first;
            }
        } else {
            SingleNode<E> prev = getNode(index - 1);
            oldElement = prev.next;
            prev.next = oldElement.next;
        }
        size--;
        return oldElement.element;
    }

    @Override
    public E set(int index, E element) {
        SingleNode<E> node = getNode(index);
        E e = node.element;
        node.element = element;
        return e;
    }

    @Override
    public E get(int index) {
        return getNode(index).element;
    }

    private SingleNode<E> getNode(int index) {
        rangeCheck(index);
        /// index = 0，不会进入循环，直接返回第一个节点first
        SingleNode<E> node = first;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        SingleNode<E> node = first;
//        for (SingleNode<E> i = node; i != null; i = i.next) { // 死循环
        for (int i = 0; i < size; i++) {
            result.append(node);
            if (i < size - 1) {
                result.append("->");
            }
            node = node.next;
        }
        return result.toString();
    }
}
